function getSiteCfg() { 
    $.getJSON(
        "/api/ocie/config_public/",
        function(json){
            var val = 'POC: <a class="POCemail" href="mailto:' +
                      json.ocie_poc_email + '">' + json.ocie_poc_name + '</a> ' +
                      'Phone: (' + json.ocie_poc_phone.substr(0,3) + ') ' +
                      json.ocie_poc_phone.substr(3,3) + '-' + 
                      json.ocie_poc_phone.substr(6,4);
            $("span.POCemail").append(val);
            val = 'To request an appointment, contact the ' +
                  json.ocie_pr_office_name + ' at <br />' +
                  '(' + json.ocie_poc_phone.substr(0,3) + ') ' +
                  json.ocie_poc_phone.substr(3,3) + '-' + 
                  json.ocie_poc_phone.substr(6,4);                 
            $("#primaryContent div.contentControls div.sidebar " +
              "span.publicSidebar:nth-child(1) h3")
                  .append(val);
        }
    ); 
}


function getDateInfo(display_date) {
    var url = '/api/dates/7day/' + ( display_date != '' ? display_date + '/' : '');
    $.getJSON(url, function(json){
        $("#ocieSchedule > caption").html(' <span class="captionDate"><a href="#" id="button_previous"><img src="/images/icons/previous.gif" /></a> ' + json.days[0].display + ' - ' + json.days[4].display + ' <a href="#" id="button_next"><img src="/images/icons/next.gif" /></a></span>Scheduled Appointments');
        var day_list = new Array();
        $("#ocieSchedule td span.appointmentPublicEvent").remove();
        $("#ocieSchedule thead th").removeClass("tableCurrentHeadColor");
        jQuery.map([4,3,2,1,0], function(i){
            $("#ocieSchedule thead th:eq(" + (i + 1) + ")").html(json.days[i].dow + '<br />' + json.days[i].display);
            $("#ocieSchedule colgroup col:eq(" + (i + 1) + ")").attr("id","osDay" + (i+2) + "Col");
            if(json.today == json.days[i].date){ 
                $("#ocieSchedule colgroup col:eq(" + (i + 1) + ")").attr("id","osTodayCol");
                $("#ocieSchedule thead th:eq(" + (i + 1) + ")").addClass("tableCurrentHeadColor");
            }
            day_list[i] = json.days[i].date;
        });
        getEventInfo(json.days[0].date,json.days[4].date,day_list);
	$("#button_previous").click( function() {
	    getDateInfo(json.previous);
            return false;
        });
	$("#button_next").click( function() {
	    getDateInfo(json.next);
            return false;
        });
    });
}


function getEventInfo(first_date,second_date,day_list) {
    var url = '/api/ocie/schedule_public/' + first_date + '/' + second_date + '/';
    $.getJSON(url, function(json){
        jQuery.map([0,1,2,3,4], function(i){
            var day_events = json[day_list[i]];
            jQuery.map(day_events, function(event){
                var vert_pos = (( (Number(event.start.substr(11,2)) * 60 + Number(event.start.substr(14,2)) ) - 435 ) / 15) - 1;
                $("#ocieSchedule tbody tr:eq(" + vert_pos + ") td:eq(" + (i + 1) + ")").append(makeEvent(event));
            });
        });
    });
}


function makeEvent(event) {
    var appt_class = 'appointmentPublicEvent ' + ( event.appointment_type != 'Booked' ? 'noAppointments' : 'publicBusy' );
    event.finish = event.finish.substr(11,2) + event.finish.substr(14,2);
    event.start = event.start.substr(11,2) + event.start.substr(14,2);
    event.finish = Number(event.finish.substr(0,2)) * 60 + Number(event.finish.substr(2,2));
    event.start = Number(event.start.substr(0,2)) * 60 + Number(event.start.substr(2,2));
    var vert_size = ((event.finish - event.start) / 15) * 23 - 4;
    var appt_style = 'height:' + vert_size + 'px;';
    var appt = '<span class="' + appt_class + '" style="' + appt_style + '">&nbsp;</span>';
    return appt;
}


$(document).ready(function() {
    getSiteCfg();
    var initial_date = ( getQueryStringVariable('date') ? getQueryStringVariable('date') + '/' : '');
    getDateInfo(initial_date);
});